package com.sita.friend.ui.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.sita.bike.R;
import com.sita.bike.model.EaseUser2;
import com.sita.bike.persistence.GroupMember;
import com.sita.bike.utils.AccountUtils;
import com.sita.bike.utils.BitmapUtils;
import com.sita.bike.utils.PersistUtils;
import com.sita.friend.FriendHelper;

import java.util.ArrayList;

/**
 * Created by lijuan zhang on 2016/2/21.
 */
public class GroupShareLocMemberAdapter extends BaseAdapter {
    public Context context;
    public ArrayList<String> arrayList_data;

    public GroupShareLocMemberAdapter(Context context, ArrayList<String> arrayList_data) {
        this.arrayList_data = arrayList_data;
        this.context = context;
    }

    @Override
    public int getCount() {
        return arrayList_data.size();
    }

    @Override
    public Object getItem(int position) {
        return arrayList_data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewholder;
        if (convertView == null) {
            viewholder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.group_sharing_hor_list_item, null);
            viewholder.img_member_avatar = (ImageView) convertView.findViewById(R.id.img_member_avatar);
            convertView.setTag(viewholder);
        } else {
            viewholder = (ViewHolder) convertView.getTag();
        }
        String mImid = arrayList_data.get(position);
        EaseUser2 easeUser2 = FriendHelper.getInstance().getContactUser(mImid);
        Bitmap avatar;

        if (easeUser2 != null) {
            avatar = AccountUtils.getUserAvatar(easeUser2.getAvatar());
        } else if (PersistUtils.getGroupMember(mImid) != null) {
            //从本地存储的member中查找用户
            GroupMember groupMember = PersistUtils.getGroupMember(mImid);
            avatar = AccountUtils.getMemberAvatar(groupMember.getAvatarUrl());
        } else {
            avatar = AccountUtils.getUserAvatar(AccountUtils.getAvatar());
        }
        //通过id获取头像，然后显示
        Bitmap bitmap = BitmapUtils.composeBitmap(R.drawable.white_cir, avatar, 50,46,1f);
        viewholder.img_member_avatar.setImageBitmap(bitmap);
        return convertView;
    }

    public void refresh(ArrayList<String> arrayList_data) {
        this.arrayList_data = arrayList_data;
        notifyDataSetChanged();
    }

    class ViewHolder {
        ImageView img_member_avatar;
    }
}
